import java.util.Arrays;
import java.util.Stack;

public class Sort {

    public static void HeapSort(int []array){
        createHeap(array);
        int end=array.length-1;
        while(end>0){
            int tmp=array[0];
            array[0]=array[end];
            array[end]=tmp;
            shiftDown(array,0,end);
            end--;
        }
    }
    public static void createHeap(int[]array){
        for (int parent = (array.length-1-1)/2; parent >=0 ; parent--) {
            shiftDown(array,parent,array.length);
        }
    }
    public static void shiftDown(int[]array,int parent,int len){
        int child=2*parent+1;
        while(child<len){
            if(child+1<len&&array[child+1]>array[child]) {
                child++;
            }
            if(array[parent]<array[child]){
                int tmp=array[parent];
                array[parent]=array[child];
                array[child]=tmp;
                parent=child;
                child=2*parent+1;
            }else{
                break;
            }
        }
    }

    public static void insertSort(int[]array,int left,int right){
        for (int i = 1; i < right; i++) {
            int tmp=array[i];
            int j = i-1;
            for (; j >left ; j--) {
                if(array[j]>tmp){
                    array[j+1]=array[j];
                }else{
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }

    /*public static void quickSort(int[]array){
        quick(array,0,array.length-1);
    }
    public static void quick(int[]array,int left,int right){
        if(left>=right){
            return;
        }

        if(right-left+1<5){
            insertSort(array,left,right);
        }

        int minValIndex=findMinValIndex(array,left,right);
        int tmp=array[left];
        array[left]=array[minValIndex];
        array[minValIndex]=tmp;
        int pivot=partition(array,left,right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,right);
    }

    public static int findMinValIndex(int[]array,int start,int end){
        int mid=start+((end-start)>>>1);
        if(array[start]<array[end]){
            if(array[mid]<array[start]){
                return start;
            }else if(array[mid]> array[end]){
                return end;
            }else{
                return mid;
            }
        }else{
            if(array[mid]>array[start]){
                return start;
            }else if(array[mid]<array[end]){
                return end;
            }else{
                return mid;
            }
        }
    }*/

    public static void quickSort(int[]array){
        int start=0;
        int end=array.length-1;
        Stack<Integer>stack=new Stack<>();
        int pivot=partition(array,start,end);
        if(pivot>start+1){
            stack.push(start);
            stack.push(pivot-1);
        }
        if(pivot<end-1){
            stack.push(pivot+1);
            stack.push(end);
        }
        while(!stack.isEmpty()){
            end=stack.pop();
            start=stack.pop();
            pivot=partition(array,start,end);
            if(pivot>start+1){
                stack.push(start);
                stack.push(pivot-1);
            }
            if(pivot<end-1){
                stack.push(pivot+1);
                stack.push(end);
            }
        }
    }

    public static int partition(int[]array,int start,int end){
        int tmp=array[start];
        while(start<end){
            while(start<end&&array[end]>=tmp){
                end--;
            }
            array[start]=array[end];
            while(start<end&&array[start]<=tmp){
                start++;
            }
            array[end]=array[start];
        }
        array[start]=tmp;
        return start;
    }


    public static void main(String[] args) {
        int[]array={3,5,22,7,56,12,23};
        //HeapSort(array);
        quickSort(array);
        System.out.println(Arrays.toString(array));
    }
}
